package com.synergy.stockflow.warehouse.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.warehouse.entity.InventoryCountDetail;
import com.synergy.stockflow.warehouse.entity.InventoryTransfer;
import com.synergy.stockflow.warehouse.service.InventoryTransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/inventory_ssss")
public class InventoryTransferController {
    @Autowired
    private InventoryTransferService inventoryTransferService;

    //查询数据库
    @GetMapping("/trees")
    private Map<String, Object> TreesAll() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<InventoryTransfer> inventoryTransfers = inventoryTransferService.list();
            result.put("success", true);
            result.put("data", inventoryTransfers);
            result.put("message", "查询成功");
        } catch (Exception a) {
            result.put("success", false);
            result.put("massage", "查询失败:" + a.getMessage());
        }
        return result;
    }

    @GetMapping
    public Map<String, Object> sag(
            @RequestParam(name = "current", defaultValue = "1") int current,
            @RequestParam(name = "size", defaultValue = "10") int size,
            @RequestParam(name = "transferId", required = false) String transferId,
            @RequestParam(name = "transferCode", required = false) String transferCode
    ) {
        Map<String, Object> result = new HashMap<>();
        try {
            QueryWrapper<InventoryTransfer> queryWrapper = new QueryWrapper<>();
            if (transferId != null && transferId.isEmpty()) {
                queryWrapper.lambda().like(InventoryTransfer::getTransferId, transferId);
            }
            if (transferCode != null && transferCode.isEmpty()) {
                queryWrapper.lambda().like(InventoryTransfer::getTransferCode, transferCode);
            }
            Page<InventoryTransfer> page = new Page<>(current, size);
            result.put("success", true);
            result.put("total", page.getPages());
            result.put("page", page.getPages());
            result.put("date", page.getRecords());
        } catch (Exception a) {
            result.put("success", false);
            result.put("massage", "分页查询失败:" + a.getMessage());
        }
        return result;
    }

    //新增
    @PostMapping("/insert")
    public Map<String, Object> insect(@RequestBody InventoryTransfer inventoryTransfer) {
        Map<String, Object> result = new HashMap<>();
        try {
            Date now = new Date();
            inventoryTransfer.setCreateTime(now);
            inventoryTransfer.setUpdateTime(now);
            boolean success = inventoryTransferService.save(inventoryTransfer);
            result.put("success", success);
            result.put("message", success ? "新增成功" : "新增失败");
            result.put("transferId", inventoryTransfer.getTransferId());
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "新增异常" + e.getMessage());
        }
        return result;
    }

    //修改
    @PutMapping("/update")
    private Map<String, Object> update(@RequestBody InventoryTransfer inventoryTransfer) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (inventoryTransfer.getTransferId() == null || inventoryTransfer.getTransferId().isEmpty()) {
                result.put("success", false);
                result.put("massage", "仓库Id不能为空");
            }
            inventoryTransfer.setCreateTime(new Date());
            boolean success = inventoryTransferService.updateById(inventoryTransfer);
            result.put("success", success);
            result.put("message", success ? "修改成功" : "修改失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "修改异常" + e.getMessage());
        }
        return result;
    }

    //删除
    @DeleteMapping("/{id}")
    private Map<String, Object> detele(@PathVariable String id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = inventoryTransferService.removeById(id);
            result.put("success", success);
            result.put("message", success ? "删除成功" : "删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除异常" + e.getMessage());
        }
        return result;
    }
}
